<?php

/**
 * fisioterapeuta actions.
 *
 * @package    fisio-ud
 * @subpackage fisioterapeuta
 * @author     Johan AR <j3valentin@gmail.com>
 */
class fisioterapeutaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->Usuarios = usuarioQuery::create()
            ->joinWithPersona()
            ->usesfGuardUserQuery()
              ->usesfGuardUserGroupQuery()
                ->usesfGuardGroupQuery()
                  ->filterByName('Fisioterapeuta')
                ->endUse()
              ->endUse()
            ->endUse()
            ->find();
  }

  public function executeShow(sfWebRequest $request)
  {
    $c = new Criteria();
//    $this->Usuario = UsuarioPeer::doSelectJoinAll($c);
            
    $this->Usuario = UsuarioPeer::retrieveByPk($request->getParameter('id'));
    $this->Usuario->getPersona()->getPersonaLocalizacions();
    $this->forward404Unless($this->Usuario);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new SFisioterapeutaForm();
    $this->form2 = new PersonaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new SFisioterapeutaForm();
    $this->form2 = new PersonaForm();

    $this->processForm($request, $this->form, $this->form2);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $Usuario = usuarioQuery::create()->findPk($request->getParameter('id'));
    $this->forward404Unless($Usuario, sprintf('Object usuario does not exist (%s).', $request->getParameter('id')));
    $this->form = new SFisioterapeutaForm($Usuario->getsfGuardUser());
    $this->form2 = new PersonaForm($Usuario->getPersona());
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $Usuario = usuarioQuery::create()->findPk($request->getParameter('id'));
    $this->forward404Unless($Usuario, sprintf('Object usuario does not exist (%s).', $request->getParameter('id')));
    $this->form = new SFisioterapeutaForm($Usuario->getsfGuardUser());
    $this->form2 = new PersonaForm($Usuario->getPersona());

    $this->processForm($request, $this->form, $this->form2);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $Usuario = usuarioQuery::create()->findPk($request->getParameter('id'));
    $this->forward404Unless($Usuario, sprintf('Object usuario does not exist (%s).', $request->getParameter('id')));
    $Usuario->delete();

    $this->redirect('fisioterapeuta/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form, sfForm $form2)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    $form2->bind($request->getParameter($form2->getName()), $request->getFiles($form2->getName()));
    
    if ($form->isValid() && $form2->isValid())
    {
      $sfGuardUser = $form->save();
//      $sfGuardUser2 = new sfGuardUserGroup();
//      $sfGuardUser2->setsfGuardGroup(sfGuardGroupQuery::create()->findOneByName("Fisioterapeuta"));
//      $sfGuardUser2->setsfGuardUser($sfGuardUser);
//      $sfGuardUser = $form->save();
      
      $Usuarios = $sfGuardUser->getUsuarios();

      $Persona = $form2->save();
      
      $Usuarios[0]->setPersona($Persona);
      $Usuarios[0]->save();
      
//      $this->redirect('fisioterapeuta/edit?id='.$Usuarios[0]->getId());
      $this->redirect('fisioterapeuta/index');
    }
  }
}